package com.wrial.kind.linkedList;
/*
 * @Author  Wrial
 * @Date Created in 13:24 2020/8/9
 * @Description
 *  奇偶链表:将奇偶分别连起来
 *  输入: 1->2->3->4->5->NULL
    输出: 1->3->5->2->4->NULL
 */

public class OddEvenList {

    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode jHead = head, jTail = head, oHead = head.next, oTail = head.next;
        while (jTail.next != null && oTail.next != null) {
            jTail.next = oTail.next;
            jTail = jTail.next;
            oTail.next = jTail.next;
            oTail = oTail.next;
        }
        jTail.next = oHead;
        return jHead;
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
